const app = getApp()
Page({
  data: {
    spinShow: false,
    info: {},
    availableTypes: [],
    courseStatus: []
  },

  onLoad: function (options) {
    let _this = this
    _this.setData({
      spinShow: true
    });
    
    app.formPost('/api/wx/student/user/current', null).then(res => {
      if (res.code == 1) {
        const subjectIds = res.response.subjectId ? 
          res.response.subjectId.split(',').map(id => Number(id.trim())) : [];
        
        // 预先计算所有课程状态
        const courseStatus = {
          1: subjectIds.includes(1),
          2: subjectIds.includes(2),
          3: subjectIds.includes(3),
          4: subjectIds.includes(4)
        };
        
        _this.setData({
          info: res.response,
          availableTypes: subjectIds,
          courseStatus: courseStatus
        }, () => {
          console.log("课程状态:", courseStatus)
          console.log("类型3是否可用:", courseStatus[3])
        });
      }
      _this.setData({
        spinShow: false
      });
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })
  },

  onShow: function () {
    // 页面显示时的逻辑
  },

  // 统一跳转到练习页面，传递类型参数
  navigateToPractice: function(event) {
    const type = event.currentTarget.dataset.type;
    const availableTypes = this.data.availableTypes;
    
    // 检查该类型是否可用
    if (!availableTypes.includes(Number(type))) {
      return; // 如果不可用，直接返回，不进行跳转
    }
    
    const typeMap = {
      '1': '焊工',
      '2': '高压', 
      '3': '低压',
      '4': '高处'
    };
    
    wx.navigateTo({
      url: `/pages/intermediate/index?type=${type}&title=${typeMap[type]}`
    })
  }
})